import { getCurrentInstance, ref } from 'vue'
import { useUniAppSystemRectInfo } from '@tuniao/tnui-vue3-uniapp/hooks'
import { tnNavPage } from '@tuniao/tnui-vue3-uniapp/utils'
import type { StoreUser } from '@/types/store/user'
import type { NavbarRectInfo } from '@tuniao/tnui-vue3-uniapp/components/navbar'
import { generateRandomNumber } from '@/utils/local-mock'
import { useScrollTransparentNavbar } from '@/hooks'
import { getScenes } from '@/services/api'
import { onLoad, onShow } from '@dcloudio/uni-app'
import storageApi from '@/utils/storage'
import { useUserStore } from '@/stores'
const userStore = useUserStore()
export const useSubPage = () => {
  const instance = getCurrentInstance()
  const { navBarInfo } = useUniAppSystemRectInfo()
  // const colorList = [
  //   {
  //     bgColor: '(255,213,128,0.4)',
  //     color: '#BE6811',
  //   },
  //   {
  //     bgColor: '(211,202,255,0.4)',
  //     color: '#8A7ED7',
  //   },
  //   {
  //     bgColor: '(179,207,255,0.4)',
  //     color: '#50B5FF',
  //   },
  //   {
  //     bgColor: '(255,198,178,0.4)',
  //     color: '#DA977F',
  //   },
  //   {
  //     bgColor: '(255,179,204,0.4)',
  //     color: '#E85E8D',
  //   },
  //   {
  //     bgColor: '(138,230,199,0.4)',
  //     color: '#73B09C',
  //   },
  // ]
  // 用户信息
  const userInfo = ref<StoreUser>()

  // const list = ref([])
  // const total = ref(0)
  // let timer = null

  // 点击获取用户信息
  const loginHandle = () => {
    setTimeout(() => {
      // uni.hideLoading()
      userInfo.value = {
        uid: '2024-03-21 09:00:00',
        nickname: '英语AI',
        username: '',
        avatar: 'https://resource.tuniaokj.com/images/avatar/xiong.jpg',
        sex: 0,
        phone: '',
        desc: '',
        birthday: '',
        profession: '',
      }
    }, 0)
  }
  loginHandle()
  // 跳转到个人信息页面
  const navUserInfoPage = () => {
    tnNavPage('/info-page/user-info/index')
  }

  // 跳转到常见问题页面
  const navCommonProblemPage = () => {
    tnNavPage('/info-page/common-problem/index')
  }

  // 跳转到会员权益页面
  const navMemberRightsPage = () => {
    tnNavPage('/info-page/member-rights/index')
  }

  // 跳转到订单列表页面
  const navOrderListPage = () => {
    tnNavPage('/order-page/order-list/index')
  }

  // onShow(() => {
  //   console.log(6666)
  //   timer = setInterval(() => {
  //     if (storageApi.get('gradeData') && storageApi.get('token')) {
  //       getData()
  //       clearInterval(timer)
  //     }
  //   }, 100)
  // })

  // const getData = () => {
  //   let gradeData = storageApi.get('gradeData')
  //   let params = {
  //     page: userStore.currentCgPage,
  //     pageSize: 6,
  //     grade: gradeData.grade,
  //     level: gradeData.level,
  //     volume: gradeData.volume,
  //   }
  //   getScenes(params).then((res) => {
  //     const { code, data } = res
  //     if (code === 0) {
  //       console.log(data)
  //       clearInterval(timer)
  //       if (data.dataList == null) {
  //         // userStore.cutCurrentCgPage(1)
  //         console.log(666)
  //         // getData()
  //         uni.showToast({
  //           title: '没有更多数据了',
  //           duration: 2000,
  //           icon: 'none',
  //         })
  //       } else {
  //         list.value = data.dataList
  //         total.value = data.total
  //         userStore.cutCurrentCgPage()
  //       }
  //     }
  //   })
  // }
  const {
    triggerElementId,
    navbarOpacity,
    init: initTransparentScroll,
    updateTargetTriggerValue,
    opacityScrollHandle,
  } = useScrollTransparentNavbar(instance)

  // 顶部导航栏初始化完成事件
  const navbarInitFinishHandle = (info: NavbarRectInfo) => {
    updateTargetTriggerValue(info.height)
  }
  // useSubPageProvide(4)

  return {
    navBarInfo,
    userInfo,
    loginHandle,
    navUserInfoPage,
    navCommonProblemPage,
    navMemberRightsPage,
    navOrderListPage,
    initTransparentScroll,
    opacityScrollHandle,
    navbarInitFinishHandle,
    // getData,
    // list,
    // total,
  }
}
